package aar.test.tessocr;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.EventMessage.EventMessage;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.loginModel;
import aar.test.tessocr.utils.ConstantPara;
import aar.test.tessocr.utils.Urls;
import clx.tessocr.ocreng.R;
import lib_zxing.activity.CodeUtils;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.Login1Activity.JSON;

public class EditCabintActivity extends AppCompatActivity {

    private LinearLayout left_layout;

    private EditText cabinet_name_edit;

    private EditText cabinet_address_edit;

    private Button grid_edit;

    private Context mContext;
    private String cabinetId = "";
    private String name = "";
    private String adress = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_cabint);
        cabinetId = getIntent().getStringExtra("cabinetId");
        mContext=this;
        cabinet_name_edit=findViewById(R.id.cabinet_name_edit);

        cabinet_address_edit=findViewById(R.id.cabinet_address_edit);

        grid_edit=findViewById(R.id.grid_edit);
        grid_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gridEdit();

            }
        });


        left_layout = findViewById(R.id.left_layout);
        left_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }






    private void gridEdit(){
        name=cabinet_name_edit.getText().toString();

        if(TextUtils.isEmpty(name)){
            Toast.makeText(mContext, "快递名称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        adress=cabinet_address_edit.getText().toString();

        if(TextUtils.isEmpty(adress)){
            Toast.makeText(mContext, "快递地址不能为空", Toast.LENGTH_SHORT).show();
            return;
        }



        Map<String ,Object> map=new HashMap<>();
        map.put("cabinetId",cabinetId);
        map.put("cabinetName",name);
        map.put("address",adress);

        Gson gg=new Gson();
        String json =gg.toJson(map);

        RequestBody body = RequestBody.create(JSON,json);
        OkGo.<LzyResponse>post(Urls.URL_EDIT_CAB)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse>(EditCabintActivity.this) {
                    @Override
                    public void onSuccess(final Response<LzyResponse> response) {


                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(null!=response.body().msg){
                                    Toast.makeText(getApplicationContext(),response.body().msg,Toast.LENGTH_SHORT).show();
                                }
                                Intent resultIntent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putString("cabinetName", name);
                                bundle.putString("cabinetId", cabinetId);
                                bundle.putString("adress", adress);
                                resultIntent.putExtras(bundle);
                                setResult(RESULT_OK, resultIntent);
                                EventBus.getDefault().post(new EventMessage().setMessage("refreshCabinetList"));
                                finish();
                            }
                        });

                    }

                    @Override
                    public void onError(final Response<LzyResponse> response) {
                        Log.e("MainActivity","onError---------------------");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(null!=response.body().msg){
                                    Toast.makeText(getApplicationContext(),response.body().msg,Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                    }
                });




    }




}
